<?php

namespace app\model\v1\repair;

use app\model\order\StoreOrder;
use app\model\user\User;
use crmeb\traits\ModelTrait;
use think\Exception;
use think\facade\Db;
use think\Model;

class AuxiliaryMaterialData extends Model
{

    use ModelTrait;

    protected $updateTime = false;
    protected $createTime = false;

    protected $hidden = [
    //    'create_time','update_time','delete_time'
    ];


    public function order()
    {
        return $this->hasOne (StoreOrder::class,'order_id','order_id');
    }


    /**
     * 代购管理列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function getMaterialDataList($param = [])
    {
        $data["data"] = self::field ("*")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [$wheretime[0], $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("material",'like', '%'.$param["word"].'%');
            })
            ->with([
                'order'=> function($query) {
                    $query->field('id,order_id,real_name,user_phone');
                },
            ])
            ->order("id", "desc")
            ->page($param["page"], $param['limit'])
            ->select()->toArray();

        $data["count"] = self::field ("*")
            ->where(function ($query) use ($param){
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('create_time', 'between', [$wheretime[0], $wheretime[1]]);
                }
                if(!empty($param["word"])) $query->where("material",'like', '%'.$param["word"].'%');
            })
            ->count();

        return $data;
    }



    /**
     * 代购管理列表 - 查看
     * @param $param
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     * @throws \think\exception\DbException
     */
    public static function detailMaterialData($param = [])
    {
        return self::field ("*")
            ->where("id", $param["id"])
            ->with([
                'order' => function ($query) {
                    $query->field(['*']);
                },
            ])
            ->find();
    }




}